//
//  LTMachineReportViewController.m
//  LearnTeacher
//
//  Created by zyl_macbookpro on 2024/4/14.
//

#import "LTMachineReportViewController.h"

#import "LTChooseCLassTermView.h"
#import "LTClassReportListView.h"
#import "LTClassAPIManager.h"
#import "LTClassModel.h"

#import "LTStudentLineView.h"
@interface LTMachineReportViewController ()

@property(nonatomic ,strong)UIScrollView *containView;

@property(nonatomic ,strong)UIView *lineView;

@property (strong, nonatomic) LTClassModel *classModel;

@property (strong, nonatomic) NSDictionary *data;

@end

@implementation LTMachineReportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"机房考试报告";
    // Do any additional setup after loading the view.
    [self setBackButtonIsHidden:NO];
    [self.view addSubview:self.containView];
    self.view.backgroundColor = LTWhiteColor;
    [self loadData];
}
-(void)loadData
{
    NSMutableDictionary *dic = @{@"start":@"2023",@"end":@"2024",@"class_id":self.classModel.class_id}.mutableCopy;
    
    [LTClassAPIManager getMachineReportWithParam:dic result:^(NSDictionary * _Nonnull dic) {
        self.data = dic[@"data"];
        [self.containView removeFromSuperview];
        self.containView = nil;
        [self.view addSubview:self.containView];
    }];
}


- (UIScrollView *)containView
{
    if (!_containView) {
        _containView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LTTopBarHeight, LTScreenWidth, LTScreenHeight- LTTopBarHeight)];
        _containView.backgroundColor = LTViewBgGray;
        _containView.showsVerticalScrollIndicator = NO;
        _containView.showsHorizontalScrollIndicator = NO;
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(16, 20, LTScreenWidth - 32,280)];
       
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0,0,LTScreenWidth - 32,280);
        gl.startPoint = CGPointMake(1, 0.5);
        gl.endPoint = CGPointMake(0, 0.5);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:102/255.0 green:154/255.0 blue:255/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:41/255.0 green:121/255.0 blue:255/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        gl.cornerRadius = 12;
        headerView.layer.shadowColor = [UIColor colorWithRed:41/255.0 green:121/255.0 blue:255/255.0 alpha:0.5000].CGColor;
        headerView.layer.shadowOffset = CGSizeMake(0,2);
        headerView.layer.shadowOpacity = 1;
        headerView.layer.shadowRadius = 4;
        headerView.layer.cornerRadius = 12;
        [headerView.layer addSublayer:gl];
        [_containView addSubview:headerView];
        
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, headerView.width - 40, 28)];
        titleLabel.font = SFontWithSize(20);
        titleLabel.text = [NSString stringWithFormat:@"%@%@",self.classModel.grade_name,@" 机房考试报告"];
        [titleLabel setTextAlignment:NSTextAlignmentCenter];
        titleLabel.textColor = LTWhiteColor;
        [headerView addSubview:titleLabel];
        [headerView addSubview:separateLineWithColor(CGRectMake(20, 112, headerView.width - 40, 0.5), rgba(238, 238, 238, 1))];
        
        [headerView addSubview:separateLineWithColor(CGRectMake(20, 194, headerView.width - 40, 0.5), rgba(238, 238, 238, 1))];
        
        double w = headerView.width/3.0;
        NSArray *titles = @[@"共计 (次)",@"市区联考",@"校级考试",@"班级考试"];
        for (int i = 0; i < 4; i++) {
            UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(i*w, 133, w, 17)];
            titleLab.font = RFontWithSize(12);
            titleLab.text =titles[i];
            [titleLab setTextAlignment:NSTextAlignmentCenter];
            titleLab.textColor = LTWhiteColor;
            [headerView addSubview:titleLab];
            
            UILabel *contentLab = [[UILabel alloc] initWithFrame:CGRectMake(i*w, 154, w, 24)];
            contentLab.font = SFontWithSize(17);
            contentLab.text = i == 0? [NSString stringWithFormat:@"%@",self.data[@"count"]]:i == 1?  [NSString stringWithFormat:@"%@",self.data[@"countCity"]]:i == 2?  [NSString stringWithFormat:@"%@",self.data[@"countSchool"]]: [NSString stringWithFormat:@"%@",self.data[@"countGrade"]];
            [contentLab setTextAlignment:NSTextAlignmentCenter];
            contentLab.textColor = LTWhiteColor;
            [headerView addSubview:contentLab];
        }
        
        NSArray *titles2 = @[@"总平均分 (分))",@"市区联考",@"校级考试",@"班级考试"];
        for (int i = 0; i < 4; i++) {
            UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(i*w, 215, w, 17)];
            titleLab.font = RFontWithSize(12);
            titleLab.text =titles[i];
            [titleLab setTextAlignment:NSTextAlignmentCenter];
            titleLab.textColor = LTWhiteColor;
            [headerView addSubview:titleLab];
            
            UILabel *contentLab = [[UILabel alloc] initWithFrame:CGRectMake(i*w, 236, w, 24)];
            contentLab.font = SFontWithSize(17);
            contentLab.text = i == 0? [NSString stringWithFormat:@"%@",self.data[@"avgScore"]]:i == 1?  [NSString stringWithFormat:@"%@",self.data[@"avgCity"]]:i == 2?  [NSString stringWithFormat:@"%@",self.data[@"avgSchool"]]: [NSString stringWithFormat:@"%@",self.data[@"avgGrade"]];
            [contentLab setTextAlignment:NSTextAlignmentCenter];
            contentLab.textColor = LTWhiteColor;
            [headerView addSubview:contentLab];
        }
        
        LTChooseCLassTermView *termView = [[LTChooseCLassTermView alloc] initWithFrame:CGRectMake((LTScreenWidth - 32 - 138)/2.0, 20, 138, 25)];
        termView.dataArray = @[@"2023-2024学年",@"2023-2024学年",@"2023-2024学年",@"2023-2024学年"];
        termView.selectedTerm = 1;
        termView.block = ^(NSInteger term) {
            [self loadData];
        };
        [headerView addSubview:termView];
        //
        LTClassReportListView *listView = [[NSBundle mainBundle] loadNibNamed:@"LTClassReportListView" owner:nil options:nil].lastObject;
        NSMutableArray *data = @[].mutableCopy;
        NSArray *array = self.data[@"list"];
        NSArray *key = @[@"date",@"people",@"countScore",@"goal",@"average"];
        
        for (NSDictionary *dic in array) {
            NSMutableArray *listArray = @[].mutableCopy;
            for (int i = 0; i < key.count; i++) {
                if (dic[key[i]]) {
                    [listArray addObject:dic[key[i]]];
                }else{
                    [listArray addObject:@""];
                }
            }
            
            [data addObject:listArray];
        }
        
        
        [listView setFrame:CGRectMake(16, headerView.bottom + 12, LTScreenWidth - 32, 34 + data.count * 28 + 43 + 16)];
        listView.titles = @[@"考试日期",@"人数",@"总分",@"得分率",@"平均分"];
        listView.data = data;
        [_containView addSubview:listView];
        
        UIView *viewBg = [[UIView alloc] init];
        viewBg.frame = CGRectMake(16,listView.bottom + 12,LTScreenWidth - 32,247);
        viewBg.backgroundColor = LTWhiteColor;
        [viewBg setAllCornersWithCornerRadius:12];
        viewBg.hidden = YES;
        [_containView addSubview:viewBg];
        
       NSMutableArray *arrayX = [NSMutableArray arrayWithCapacity:0];
        NSMutableArray *arrayY = [NSMutableArray arrayWithCapacity:0];
        
        if (self.data[@"linear"][@"date"]) {
            [arrayX addObjectsFromArray:self.data[@"linear"][@"date"]];
        }
        if (self.data[@"linear"][@"goal"]) {
            [arrayY addObjectsFromArray:self.data[@"linear"][@"goal"]];
        }
        if (arrayX.count > 0&&arrayY.count > 0&&arrayX.count == arrayY.count) {
            LTStudentLineView *maskLineView = [[LTStudentLineView alloc]initWithFrame:CGRectMake(16, 0, LTScreenWidth - 32 - 35, 247)];
            maskLineView.unitStr = @"得分率(%)";
            [viewBg addSubview:maskLineView];
            maskLineView.arrayX = arrayX;
            maskLineView.arrayY = arrayY;
            viewBg.hidden = NO;
        }
       
        
    }
    return _containView;
}
- (UIView *)lineView
{
    if(!_lineView)
    {
        _lineView = [[UIView alloc] init];
        _lineView.frame = CGRectMake(95,40,28,3);
        _lineView.backgroundColor = rgba(41, 121, 255, 1);
        [_lineView setAllCornersWithCornerRadius:1.5];
    }
    return _lineView;
}

- (void)setParaObject:(NSObject *)paraObject
{
    self.classModel = (LTClassModel *)paraObject;
}
@end
